package book;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;

import common.DBManager;

public class BookBean {

	public static final int PAGE_MAX = 20;

	private int id;
	private int genreId;
	private int publisherId;
	private String title;
	private String writer;
	private String content;
	private int newestId;
	private String publicationDate;
	private String picture;
	private int rentalId;
	private boolean deleteFlag;		//削除フラグ	引数『0』：false、引数『1』：true
	private String isbn;
	private boolean openFlag;		//公開フラグ	引数『0』非公開：false、引数『1』公開：true
	@SuppressWarnings("unused")
	private String genre;
	@SuppressWarnings("unused")
	private String publisher;
	private UpdateBean updateBean;
	private RentalBean rentalBean;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getGenreId() {
		return genreId;
	}
	public void setGenreId(int genreId) {
		this.genreId = genreId;
	}
	public int getPublisherId() {
		return publisherId;
	}
	public void setPublisherId(int publisherId) {
		this.publisherId = publisherId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer.replaceAll(" ", "");
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public int getNewestId() {
		return newestId;
	}
	public void setNewestId(int newestId) {
		this.newestId = newestId;
	}
	public String getPublicationDate() {
		return publicationDate;
	}
	public void setPublicationDate(String publicationDate) {
		this.publicationDate = publicationDate;
	}
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	public int getRentalId() {
		return rentalId;
	}
	public void setRentalId(int rentalId) {
		this.rentalId = rentalId;
	}
	public boolean isDeleteFlag() {
		return deleteFlag;
	}
	public void setDeleteFlag(int deleteFlag) {
		if(deleteFlag == 0) {
			this.deleteFlag = false;
		} else {
			this.deleteFlag = true;
		}
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	public boolean isOpenFlag() {
		return openFlag;
	}
	public void setOpenFlag(int openFlag) {
		if(openFlag == 0) {
			this.openFlag = false;
		} else {
			this.openFlag = true;
		}
	}
	public String getGenre() throws ServletException {

		String sql = "select name from genres where id = " + this.getGenreId();
		return getData(sql);
	}
	public String getPublisher() throws ServletException {

		String sql = "select name from publishers where id = " + this.getPublisherId();
		return getData(sql);
	}
	public UpdateBean getUpdateBean() {
		return updateBean;
	}
	public RentalBean getRentalBean() {
		return rentalBean;
	}

	public String getData(String sql) throws ServletException {

		Connection con = null;
		Statement smt = null;

		try {
			con = DBManager.getConnection();
			smt = con.createStatement();
			ResultSet rs = smt.executeQuery(sql);
			rs.first();

			return rs.getString("NAME");

		} catch(SQLException e) {
			throw new ServletException(e);
		} finally {
			if(smt != null) {
				try {
					smt.close();
				} catch(SQLException ignore) {
					// none
				}
			}
			if(con != null) {
				try {
					con.close();
				} catch(SQLException ignore) {
					// none
				}
			}
		}

	}
}
